/**
 * serial_stream.c
 *
 *  Created on: 2019-11-26
 *      Author: chenshisheng
 */

#include "stream/serial_stream.h"

static int _Put(void *stream, unsigned char b)
{
	SerialStream_t *ss = stream;
    Serial_t serial = (Serial_t)ss->userData;

    Serial_SendByte(serial, b);
    return 1;
}

static int _Get(void *stream, unsigned char *pb)
{
	SerialStream_t *ss = stream;
    Serial_t serial = (Serial_t)ss->userData;

    return (Serial_RecvByte(serial, pb, 0) == 0) ? 1 : 0;
}

static void _Flush(void *stream)
{
    SerialStream_t *ss = stream;
    Serial_t serial = (Serial_t)ss->userData;

    while(! Serial_IoCtl(serial, Serial_Ctl_IsTxDone))
    {
        IDLE();
    }
}

int SerialStream_Init(SerialStream_t *ss, Serial_t serial)
{
    return IoStream_InitWithoutBuffer((IoStream_t *)ss, _Put, _Get, _Flush, serial);
}
